const { ccclass, property } = cc._decorator;
import WinFormBase from '../WinFormBase';
import StoreControl from './StoreControl';
import taskControl from './taskControl';
import SelectModel from './SelectModel';

@ccclass
export default class bottomControl extends cc.Component {
    @property(cc.Node)
    SkillPage: cc.Node = null;
    @property(cc.Node)
    StorePage: cc.Node = null;
    @property(cc.Node)
    TaskPage: cc.Node = null;

    @property(cc.Node)
    selecteNode: cc.Node = null;

    @property(cc.Node)
    SkillNode: cc.Node = null;//技能背包按钮
    @property(cc.Node)
    StoreNode: cc.Node = null;//商店背包按钮
    @property(cc.Node)
    TaskNode: cc.Node = null;//任务背包按钮

    _SelectModel: SelectModel = null;
    ShakeList: Array<cc.Node> = new Array<cc.Node>();

    onLoad() {
        this._SelectModel = this.selecteNode.getComponent(SelectModel);
        this.schedule(this.ShakeNode, 8);
    }

    setSkillPage() {
        let fromNode = new cc.Node();
        let fromControl = fromNode.addComponent(WinFormBase);
        var pageViewEventHandler = new cc.Component.EventHandler();
        pageViewEventHandler.target = this.node;
        pageViewEventHandler.component = "bottomControl"
        pageViewEventHandler.handler = "updateSkillLoad";
        fromControl.CloseEvents.push(pageViewEventHandler);
        fromControl.isBgClose = true;
        fromControl.mainNode = this.SkillPage;
        this.node.addChild(fromNode);
    }
    //回调
    updateSkillLoad() {
        let p = gameHelper.getInstance().skillParam;
        if (p.new) {
            p.new = false;
            gameHelper.getInstance().skillParam = p;
            this.unShakeNode(this.SkillNode);
        }
        this._SelectModel.updateSkill();
    }
    SkillHint() {
        let p = gameHelper.getInstance().skillParam;
        if (p.new) {
            this.addShakeNode(this.SkillNode);
        }
    }

    setStorePage() {
        let control = this.StorePage.getComponent(StoreControl);
        control.init();
        let fromNode = new cc.Node();
        let fromControl = fromNode.addComponent(WinFormBase);
        var pageViewEventHandler = new cc.Component.EventHandler();
        pageViewEventHandler.target = this.node;
        pageViewEventHandler.component = "bottomControl"
        pageViewEventHandler.handler = "updateStoreLoad";
        fromControl.CloseEvents.push(pageViewEventHandler);
        fromControl.isBgClose = true;
        fromControl.mainNode = this.StorePage;
        this.node.addChild(fromNode);
    }
    //回调
    updateStoreLoad() {

    }
    StoreHint() {
        this.SkillHint();
    }

    settaskPage() {
        let control = this.TaskPage.getComponent(taskControl);
        control.init();
        let fromNode = new cc.Node();
        let fromControl = fromNode.addComponent(WinFormBase);
        var pageViewEventHandler = new cc.Component.EventHandler();
        pageViewEventHandler.target = this.node;
        pageViewEventHandler.component = "bottomControl"
        pageViewEventHandler.handler = "updateTaskLoad";
        fromControl.CloseEvents.push(pageViewEventHandler);
        fromControl.isBgClose = true;
        fromControl.mainNode = this.TaskPage;
        this.node.addChild(fromNode);
    }
    //回调
    updateTaskLoad() {
        let contr = this.TaskPage.getComponent(taskControl);
        if (contr.updateTask()) {
            this.addShakeNode(this.TaskNode);
        }
        else
            this.unShakeNode(this.TaskNode);
    }
    TaskHint() {
        let contr = this.TaskPage.getComponent(taskControl);
        if (contr.updateTask()) {
            this.addShakeNode(this.TaskNode);
        }
    }
    addShakeNode(node: cc.Node) {
        let index = this.ShakeList.indexOf(node);
        if (index < 0)
            this.ShakeList.push(node);
    }
    unShakeNode(node: cc.Node) {
        let index = this.ShakeList.indexOf(node);
        if (index > -1) {
            this.ShakeList.splice(index, 1);
        }
    }
    ShakeNode() {
        this.SkillHint();
        this.TaskHint();
        if (this.ShakeList?.length > 0) {
            this.ShakeList.forEach(element => {
                this.setTween(element);
            });
        }
    }
    setTween(node: cc.Node) {
        cc.tween(node)
            .to(0.1, { angle: 20 })
            .to(0.3, { angle: -25 })
            .to(0.3, { angle: 15 })
            .to(0.1, { angle: 0 })
            .start();
    }
}
